package Beavers;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.LayerManager;

/**
 * @author Ferry
 */
public class mainCanvas extends Canvas implements CommandListener {

    /**
     * constructor
     */
    public int stateManager;
    public SampleSprite splash, bg;
    public LayerManager lmng;
    SampleSprite as;
    int x = 0;
    boolean lanjut = true;
    Zakat midlet;
    Timer timer = new Timer();
    public int posisi = 0;
    public int TotalIncome = 0;
    public int TotalOutcome = 0;
    private int hasil = 0;

    public mainCanvas() {
        try {
            bg = new SampleSprite(Image.createImage("/res/bg.png"));

            splash = new SampleSprite(Image.createImage("/res/splash.png"));

            ManageState(1);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void setMidlet(Zakat midlet) {
        this.midlet = midlet;
    }

    /**
     * paint
     */
    public void paint(Graphics g) {

        setFullScreenMode(true);
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        lmng.paint(g, 0, 0);
        if (stateManager == 2) {
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            if (posisi == 0) {
                g.setColor(0, 0, 0);
            } else {
                g.setColor(255, 255, 255);
            }
            g.drawString(Integer.toString(TotalIncome), 168, 75, Graphics.LEFT | Graphics.TOP);
            if (posisi == 1) {
                g.setColor(0, 0, 0);
            } else {
                g.setColor(255, 255, 255);
            }
            g.drawString(Integer.toString(TotalOutcome), 168, 96, Graphics.LEFT | Graphics.TOP);

            g.setColor(255, 255, 255);
//            setHasil(TotalIncome - TotalOutcome);
            g.drawString(Integer.toString(getHasil()), 168, 117, Graphics.LEFT | Graphics.TOP);
            int zakat = (int) (getHasil() * 2.5 / 100);
            g.drawString(Integer.toString(zakat), 168, 138, Graphics.LEFT | Graphics.TOP);

            g.setColor(0, 0, 0);
            g.drawString("Exit", getWidth() - 2, getHeight() - 2, Graphics.BOTTOM | Graphics.RIGHT);

            g.drawString("Reset", 0, getHeight() - 2, Graphics.BOTTOM | Graphics.LEFT);

        }

    }

    public void ManageState(int State) {
        if (State == 1) { // state for splash screen
            timer.schedule(new CountDown(), 2000);
            splash.setPosition(0, 0);
            lmng = new LayerManager();
            lmng.append(splash);

            repaint();
        } else if (State == 2) { // state for main menu
            System.out.println("masuk state 2");
            bg.setPosition(0, 0);
            lmng = new LayerManager();
            lmng.remove(splash);
            lmng.append(bg);
            this.stateManager = State;
            repaint();
        }

    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
        if (keyCode == -1) {
            if (posisi > 0) {
                posisi--;
            }
        } else if (keyCode == -2) {
            if (posisi < 1) {
                posisi++;
            }
        }
        repaint();
        if (keyCode == -5) {
            midlet.masukTextbox(posisi);

        }

        if (keyCode == -7) {
            midlet.exitMIDlet();
        }
        if (keyCode == -6) {
            TotalIncome = 0;
            TotalOutcome = 0;
            setHasil(0);
            posisi = 0;
            repaint();
        }

    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
    }

    private void dismiss() {
        timer.cancel();
        ManageState(2);
    }

    /**
     * @return the hasil
     */
    public int getHasil() {
        return hasil;
    }

    /**
     * @param hasil the hasil to set
     */
    public void setHasil(int hasil) {
        this.hasil = hasil;
    }

    // ----------------------------------------
    private class CountDown extends TimerTask {

        public void run() {
            dismiss();
        }
    }
}
